package test;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.alibaba.fastjson.JSONObject;
import com.lt.dao.ChildDao;
import com.lt.dao.HumanDao;
import com.lt.model.Child;
import com.lt.model.Human;
import com.mongodb.BasicDBObject;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:spring-context.xml" })
public class UserDaoTestController {


	@Resource
	private HumanDao humanDao;

	@Resource
	private ChildDao childDao;


	/**
	 * 新增
	 */
	@Test
	public void save() {

		for (int i = 0; i < 100; i++) {
			new Thread(new Runnable() {

				@Override
				public void run() {
					for (int i = 100; i < 2000; i++) {
						// 1.基本信息
						Human human = new Human();
						human.setName(String.valueOf(i));
						if (i % 3 == 0) {
							human.setSex("女");
						} else {
							human.setSex("男");
						}
						human.setSource(new Random().nextInt(100));
						if (i % 4 == 0) {
							human.setDept("product");
						} else {
							human.setDept("devlop");
						}

						// 保存孩子
						List<Child> childs = new ArrayList<>();
						for (int j = 0; j < new Random().nextInt(5); j++) {
							Child child = new Child();
							child.setName(String.valueOf(i) + "." + j);
							if (j % 2 == 0) {
								child.setSex("女");
							} else {
								child.setSex("男");
							}
							childDao.save(child);
							childs.add(new Child(child.getId()));
						}
						human.setChilds(childs);
						List<Integer> hasNum = new ArrayList<>();
						for (int j = 0; j < new Random().nextInt(5); j++) {
							hasNum.add(new Random().nextInt(100));
						}
						human.setHasNum(hasNum);
						humanDao.save(human);
					}
				}
			}).start();
		}

		while (true) {

		}

	}

	/**
	 * 修改
	 */
	@Test
	public void update() {
		// update对象方法
		Update update = new Update();
		update.set("name", "小莉莉");
		humanDao.update(Criteria.where("name").is("小莉"), update);

	}

	/**
	 * 根据条件删除删除
	 */
	@Test
	public void delete() {
		humanDao.delete(Criteria.where("name").is("小莉莉").and("age").is(24));
	}

	/**
	 * 根据ID查找一个对象
	 */
	@Test
	public void queryById() {
		Human human = humanDao.queryById("5b80effd9358b7812c980011", null, new String[] { "id", "childs" });
		System.out.println(JSONObject.toJSONString(human));
	}

	/**
	 * 根据ID查找一个对象指定属性
	 */
	@Test
	public void findById() {
		Human human = humanDao.queryById("5b80cf6a935830163e4166e3", new String[] { "id", "name", "childs" }, null);
		System.out.println(JSONObject.toJSONString(human));
	}

	/**
	 * 根据条件查找一个对象
	 */
	@Test
	public void queryOne() {
		Human human = humanDao.queryOne(Criteria.where("name").is("小丽"));
		System.out.println(JSONObject.toJSONString(human));
	}

	/**
	 * 查找全部
	 */
	@Test
	public void queryList() {
		long s = System.currentTimeMillis();
		List<Human> human = humanDao.queryList(new Criteria(), null, null, new String[] { "childs" });
		System.out.println(human.get(human.size() - 1).getId());
		System.out.println("耗时:" + (s - (s = System.currentTimeMillis())));
	}

	/**
	 * 分页查找全部
	 */
	@Test
	public void queryPage() {
		final Pageable page = new PageRequest(0, 4);
		List<Human> human = humanDao.queryPage(new Criteria(), page, null);
		System.out.println(JSONObject.toJSONString(human));
		System.out.println(human.size());
	}


	/**
	 * 分组查询,个数统计
	 */
	@Test
	public void groupByCount() {
		List<BasicDBObject> human = humanDao.groupByCount(new Criteria(), null);
		System.out.println(JSONObject.toJSONString(human));
	}

	/**
	 * 分组查询,sum汇总
	 */
	@Test
	public void groupBySum() {
		List<BasicDBObject> human = humanDao.groupBySum(new Criteria(), "source", null);
		System.out.println(JSONObject.toJSONString(human));
	}

	/**
	 * 分组查询,avg平均
	 */
	@Test
	public void groupByAvg() {
		List<BasicDBObject> human = humanDao.groupByAvg(new Criteria(), "source", null);
		System.out.println(JSONObject.toJSONString(human));
	}

	/**
	 * 分组查询,avg平均
	 */
	@Test
	public void groupByMax() {
		List<BasicDBObject> human = humanDao.groupByMax(new Criteria(), "source", null);
		System.out.println(JSONObject.toJSONString(human));
	}

	/**
	 * or条件查询
	 */
	@Test
	public void orSearch() {
		Criteria criteria = new Criteria();
		// or条件存放
		Criteria[] or = new Criteria[2];
		// criteria.and("sex").is("男");
		or[0] = new Criteria().orOperator(Criteria.where("name").is("105"), Criteria.where("name").is("150"));
		or[1] = new Criteria().orOperator(Criteria.where("source").is(60), Criteria.where("source").is(68));
		criteria.andOperator(or);
		List<Human> human = humanDao.queryList(criteria, null, new String[] { "name", "source", "sex" }, null);
		for (Human human2 : human) { 
			System.out.println(JSONObject.toJSONString(human2));
		}
		System.out.println(human.size());
	}



}